﻿using System;
using System.Web.Configuration;
using System.Web.Security;

namespace Salient.Web.Administration.Security
{
    public partial class security : SecurityPage
    {
        public void BackToPreviousPage(object sender, EventArgs e)
        {
            ReturnToPreviousPage(sender, e);
        }

        protected void DisableRoleManager()
        {
            System.Configuration.Configuration config = OpenWebConfiguration(ApplicationPath);
            var roleSection = (RoleManagerSection) config.GetSection("system.web/roleManager");
            roleSection.Enabled = false;
            SaveConfig(config);
        }

        protected void EnableRoleManager()
        {
            System.Configuration.Configuration config = OpenWebConfiguration(ApplicationPath);
            var roleSection = (RoleManagerSection) config.GetSection("system.web/roleManager");
            roleSection.Enabled = true;
            SaveConfig(config);
        }


        public void Page_Init()
        {
            if (IsWindowsAuth())
            {
                userManagementDisabler.ActiveViewIndex = 1;
            }
            string appPath = ApplicationPath;
            if (appPath != null && appPath.Length > 0)
            {
                application.Text = String.Format((string) GetLocalResourceObject("SecurityManagementForSite"), appPath);
            }
        }

        public void Page_Load()
        {
            if (!IsPostBack)
            {
                CurrentUser = null;

                try
                {
                    System.Configuration.Configuration config = OpenWebConfiguration(ApplicationPath);
                    // this call is only to detect the validity of the config setting
                    var roleSection = (RoleManagerSection) config.GetSection("system.web/roleManager");
                    bool temp = roleSection.Enabled;
                }
                catch (Exception e)
                {
                    SetCurrentException(Context, e);
                    // do not prepend ~/ to this path, since its not at the root
                    Response.Redirect("security0.aspx");
                }

                UpdateProviderUI();
            }
        }

        public void ToggleRoles(object sender, EventArgs e)
        {
            if (IsRoleManagerEnabled())
            {
                DisableRoleManager();
            }
            else
            {
                EnableRoleManager();
            }
            UpdateProviderUI();
        }


        private void UpdateProviderUI()
        {
            try
            {
                int total = 0;
                var users =
                    (MembershipUserCollection)
                    CallWebAdminHelperMethod(true, "GetAllUsers", new object[] {0, Int32.MaxValue, total},
                                             new[] {typeof (int), typeof (int), Type.GetType("System.Int32&")});
                string[] roles = null;
                try
                {
                    roles = (string[]) CallWebAdminHelperMethod(false, "GetAllRoles", new object[] {}, null);
                }
                catch
                {
                    // will throw if roles not enabled
                }

                userCount.Text = users.Count.ToString();
                if (IsRoleManagerEnabled())
                {
                    if (roles != null)
                    {
                        roleCount.Text = roles.Length.ToString();
                    }
                    else
                    {
                        roleCount.Text = "0";
                    }
                    roleCount.Visible = true;
                    roleMessage.Text = (string) GetLocalResourceObject("ExistingRoles");
                    waLink5.Visible = true;
                    waLabel5.Visible = false;
                    toggleRoles.Text = (string) GetLocalResourceObject("DisableRoles");
                }
                else
                {
                    roleCount.Visible = false;
                    roleMessage.Text = (string) GetLocalResourceObject("RolesNotEnabled");
                    // Cannot disable a hyperlink, so "replace" it with a label.
                    waLink5.Visible = false;
                    waLabel5.Visible = true;
                    toggleRoles.Text = (string) GetLocalResourceObject("EnableRoles");
                }
            }
            catch (Exception e)
            {
                SetCurrentException(Context, e);
                // do not prepend ~/ to this path, since its not at the root
                Response.Redirect("security0.aspx");
                    // States that there is a problem with the selected data store and redirects to chooseProvider.aspx
            }
        }
    }
}